/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Actions;

import Randomizer.Hash;
import Randomizer.Randomizer;
import Users.Customer;
import com.opensymphony.xwork2.ActionSupport;
import db.DBConnector;
import java.security.MessageDigest;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Adjell Ian
 */
public class Register extends ActionSupport {

    private Customer customer;
    private DBConnector db = null;
    private String message = "";
    private CallableStatement callable = null;
    private String custfname,custmiddle,custlname,custemail,custusername,custpassword;
    private String custdeliverystreet,custdeliverysubdivision,custdeliverycity,custdeliverycountry;
    private int custdeliveryno,custdeliverypostal;
    private String custcreditname;
    private int custcreditissuer,custcreditnum;
    private Date custcreditexpdate;
    private String custcreditbillingstreet, custcreditbillingsubdivision, custcreditbillingcity, custcreditbillingcountry;
    private int custcreditbillingno, custcreditbillingpostal;
    
    private Hash hasher;
    
    public Register() {
    }

    @Override
    public String execute() throws Exception {
        PreparedStatement pstmt = null;
        Randomizer rand = new Randomizer();
        int customerID = rand.GenerateNumber();
        int deladdressID;
        int creditID;
        int billingID;
        int i = 1;
        db = new DBConnector();
        Connection conn = null;

        try {
            conn = db.getConnection();

            MessageDigest md5 = MessageDigest.getInstance("SHA-256");
            md5.update(getCustpassword().getBytes());
            
            byte byteData1[] = md5.digest();
            
            StringBuffer stringbuf = new StringBuffer();
            
            for(int c = 0; c<byteData1.length;c++){
                stringbuf.append(Integer.toString((byteData1[c] & 0xff) + 0x100, 16).substring(1));
            }
            
            String createCall = "{call insert_customer(?,?,?,?,?,?,?,?) }";
//---------- calls the insert customer procedure--------------------------------            
            callable = conn.prepareCall(createCall);
            callable.setInt(1, customerID);
            callable.setString(2, custfname);
            callable.setString(3, custmiddle);
            callable.setString(4, custlname);
            callable.setString(5, custpassword);
            callable.setString(6, custusername);
            callable.setString(7, custemail);
            callable.setBoolean(8, true);
            callable.executeUpdate();




            String calladd = "{ call insert_address(?,?,?,?,?,?,?) }";

//---------- call insert address stored procedure-------------------------------   
            deladdressID = rand.GenerateNumber();
            callable = conn.prepareCall(calladd);
            callable.setInt(1, deladdressID);
            callable.setInt(2, custdeliveryno);
            callable.setString(3, custdeliverystreet);
            callable.setString(4, custdeliverysubdivision);
            callable.setString(5, custdeliverycity);
            callable.setInt(6, custdeliverypostal);
            callable.setString(7, custdeliverycountry);
            callable.executeUpdate();
//---------- call insert deliver procedure--------------------------------------
            String calldeliver = "{ call insert_delivery(?,?) }";

            callable = conn.prepareCall(calldeliver);
            callable.setInt(1, customerID);
            callable.setInt(2, deladdressID);
            callable.executeUpdate();

//----------calls the insert credit stored procedure----------------------------
            String callcredit = "{ call insert_credit(?,?,?,?,?) }";
            creditID = rand.GenerateNumber();

            callable = conn.prepareCall(callcredit);
            callable.setInt(1, creditID);
            callable.setInt(2, custcreditnum);
            callable.setString(3, custcreditname);
            callable.setInt(4, custcreditissuer);
            callable.setDate(5, custcreditexpdate);
            callable.executeUpdate();


            String callowner = "{ call insert_owner(?,?) }";

            callable = conn.prepareCall(callowner);
            callable.setInt(1, creditID);
            callable.setInt(2, customerID);
            callable.executeUpdate();

            billingID = rand.GenerateNumber();

            String callbilling = "{ call insert_address(?,?,?,?,?,?,?) }";


            callable = conn.prepareCall(callbilling);
            callable.setInt(1, billingID);
            callable.setInt(2, custcreditbillingno);
            callable.setString(3, custcreditbillingstreet);
            callable.setString(4, custcreditbillingsubdivision);
            callable.setString(5, custcreditbillingcity);
            callable.setInt(6, custcreditbillingpostal);
            callable.setString(7, custcreditbillingcountry);
            callable.executeUpdate();


            String callbilladd = "{ call insert_billadd(?,?) }";

            callable = conn.prepareCall(callbilladd);
            callable.setInt(1, billingID);
            callable.setInt(2, creditID);
            callable.executeUpdate();

            conn.close();

            return SUCCESS;
        } catch (Exception e) {
            Logger.getLogger(Register.class.getName()).log(Level.SEVERE, null, e);
            
            return INPUT;
        }
    }

    /**
     * @return the customer
     */
    public Customer getCustomer() {
        return customer;
    }

    /**
     * @param customer the customer to set
     */
    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    /**
     * @return the message
     */
    public String getMessage() {
        return message;
    }

    /**
     * @param message the message to set
     */
    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * @return the custfname
     */
    public String getCustfname() {
        return custfname;
    }

    /**
     * @param custfname the custfname to set
     */
    public void setCustfname(String custfname) {
        this.custfname = custfname;
    }

    /**
     * @return the custmiddle
     */
    public String getCustmiddle() {
        return custmiddle;
    }

    /**
     * @param custmiddle the custmiddle to set
     */
    public void setCustmiddle(String custmiddle) {
        this.custmiddle = custmiddle;
    }

    /**
     * @return the custlname
     */
    public String getCustlname() {
        return custlname;
    }

    /**
     * @param custlname the custlname to set
     */
    public void setCustlname(String custlname) {
        this.custlname = custlname;
    }

    /**
     * @return the custemail
     */
    public String getCustemail() {
        return custemail;
    }

    /**
     * @param custemail the custemail to set
     */
    public void setCustemail(String custemail) {
        this.custemail = custemail;
    }

    /**
     * @return the custusername
     */
    public String getCustusername() {
        return custusername;
    }

    /**
     * @param custusername the custusername to set
     */
    public void setCustusername(String custusername) {
        this.custusername = custusername;
    }

    /**
     * @return the custpassword
     */
    public String getCustpassword() {
        return custpassword;
    }

    /**
     * @param custpassword the custpassword to set
     */
    public void setCustpassword(String custpassword) {
        this.custpassword = custpassword;
    }

    /**
     * @return the custdeliverystreet
     */
    public String getCustdeliverystreet() {
        return custdeliverystreet;
    }

    /**
     * @param custdeliverystreet the custdeliverystreet to set
     */
    public void setCustdeliverystreet(String custdeliverystreet) {
        this.custdeliverystreet = custdeliverystreet;
    }

    /**
     * @return the custdeliverysubdivision
     */
    public String getCustdeliverysubdivision() {
        return custdeliverysubdivision;
    }

    /**
     * @param custdeliverysubdivision the custdeliverysubdivision to set
     */
    public void setCustdeliverysubdivision(String custdeliverysubdivision) {
        this.custdeliverysubdivision = custdeliverysubdivision;
    }

    /**
     * @return the custdeliverycity
     */
    public String getCustdeliverycity() {
        return custdeliverycity;
    }

    /**
     * @param custdeliverycity the custdeliverycity to set
     */
    public void setCustdeliverycity(String custdeliverycity) {
        this.custdeliverycity = custdeliverycity;
    }

    /**
     * @return the custdeliverycountry
     */
    public String getCustdeliverycountry() {
        return custdeliverycountry;
    }

    /**
     * @param custdeliverycountry the custdeliverycountry to set
     */
    public void setCustdeliverycountry(String custdeliverycountry) {
        this.custdeliverycountry = custdeliverycountry;
    }

    /**
     * @return the custdeliveryno
     */
    public int getCustdeliveryno() {
        return custdeliveryno;
    }

    /**
     * @param custdeliveryno the custdeliveryno to set
     */
    public void setCustdeliveryno(int custdeliveryno) {
        this.custdeliveryno = custdeliveryno;
    }

    /**
     * @return the custdeliverypostal
     */
    public int getCustdeliverypostal() {
        return custdeliverypostal;
    }

    /**
     * @param custdeliverypostal the custdeliverypostal to set
     */
    public void setCustdeliverypostal(int custdeliverypostal) {
        this.custdeliverypostal = custdeliverypostal;
    }

    /**
     * @return the custcreditname
     */
    public String getCustcreditname() {
        return custcreditname;
    }

    /**
     * @param custcreditname the custcreditname to set
     */
    public void setCustcreditname(String custcreditname) {
        this.custcreditname = custcreditname;
    }

    /**
     * @return the custcreditissuer
     */
    public int getCustcreditissuer() {
        return custcreditissuer;
    }

    /**
     * @param custcreditissuer the custcreditissuer to set
     */
    public void setCustcreditissuer(int custcreditissuer) {
        this.custcreditissuer = custcreditissuer;
    }

    /**
     * @return the custcreditnum
     */
    public int getCustcreditnum() {
        return custcreditnum;
    }

    /**
     * @param custcreditnum the custcreditnum to set
     */
    public void setCustcreditnum(int custcreditnum) {
        this.custcreditnum = custcreditnum;
    }

    /**
     * @return the custcreditexpdate
     */
    public Date getCustcreditexpdate() {
        return custcreditexpdate;
    }

    /**
     * @param custcreditexpdate the custcreditexpdate to set
     */
    public void setCustcreditexpdate(Date custcreditexpdate) {
        this.custcreditexpdate = custcreditexpdate;
    }

    /**
     * @return the custcreditbillingstreet
     */
    public String getCustcreditbillingstreet() {
        return custcreditbillingstreet;
    }

    /**
     * @param custcreditbillingstreet the custcreditbillingstreet to set
     */
    public void setCustcreditbillingstreet(String custcreditbillingstreet) {
        this.custcreditbillingstreet = custcreditbillingstreet;
    }

    /**
     * @return the custcreditbillingsubdivision
     */
    public String getCustcreditbillingsubdivision() {
        return custcreditbillingsubdivision;
    }

    /**
     * @param custcreditbillingsubdivision the custcreditbillingsubdivision to set
     */
    public void setCustcreditbillingsubdivision(String custcreditbillingsubdivision) {
        this.custcreditbillingsubdivision = custcreditbillingsubdivision;
    }

    /**
     * @return the custcreditbillingcity
     */
    public String getCustcreditbillingcity() {
        return custcreditbillingcity;
    }

    /**
     * @param custcreditbillingcity the custcreditbillingcity to set
     */
    public void setCustcreditbillingcity(String custcreditbillingcity) {
        this.custcreditbillingcity = custcreditbillingcity;
    }

    /**
     * @return the custcreditbillingcountry
     */
    public String getCustcreditbillingcountry() {
        return custcreditbillingcountry;
    }

    /**
     * @param custcreditbillingcountry the custcreditbillingcountry to set
     */
    public void setCustcreditbillingcountry(String custcreditbillingcountry) {
        this.custcreditbillingcountry = custcreditbillingcountry;
    }

    /**
     * @return the custcreditbillingno
     */
    public int getCustcreditbillingno() {
        return custcreditbillingno;
    }

    /**
     * @param custcreditbillingno the custcreditbillingno to set
     */
    public void setCustcreditbillingno(int custcreditbillingno) {
        this.custcreditbillingno = custcreditbillingno;
    }

    /**
     * @return the custcreditbillingpostal
     */
    public int getCustcreditbillingpostal() {
        return custcreditbillingpostal;
    }

    /**
     * @param custcreditbillingpostal the custcreditbillingpostal to set
     */
    public void setCustcreditbillingpostal(int custcreditbillingpostal) {
        this.custcreditbillingpostal = custcreditbillingpostal;
    }

    /**
     * @return the custfname
     */
    

  
}
